package com.guigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.eduservice.entity.*;
import com.guigu.eduservice.mapper.PurchaseDemandMapper;
import com.guigu.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.eduservice.vo.EquipmentVo;
import com.guigu.eduservice.vo.PurchaseDemandDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 求购信息表（存储买家发布的二手机械求购需求） 服务实现类
 * </p>
 *
 * @author Xiaosong
 * @since 2025-10-10
 */
@Service
public class PurchaseDemandServiceImpl extends ServiceImpl<PurchaseDemandMapper, PurchaseDemand> implements PurchaseDemandService {
@Autowired
private PurchaseDemandMapper purchaseDemandMapper;
@Autowired
private PurchaseDemandService purchaseDemandService;
@Autowired
private EquipmentTypeService equipmentTypeService;
    @Override
    public List<PurchaseDemand> getNewPurchaseDemand() {
        return purchaseDemandMapper.getNewPurchaseDemand();
    }
    private List<Long> getAllChildTypeIds(Long parentId) {
        List<Long> typeIds = new ArrayList<>();
        typeIds.add(parentId);
        QueryWrapper<EquipmentType> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        List<EquipmentType> childTypes = equipmentTypeService.list(wrapper);
        for (EquipmentType child : childTypes) {
            typeIds.addAll(getAllChildTypeIds(child.getTypeId()));
        }
        return typeIds;
    }
    @Override
    public PageInfo<PurchaseDemand> queryPurchaseDemandPage(Integer pageNo, Integer pageSize, PurchaseDemandDto vo) {
        System.out.println("开始查询");
        PageHelper.startPage(pageNo, pageSize);
        QueryWrapper<PurchaseDemand> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_status",1);
        //设备类型筛选
        Long selectedTypeId = vo.getEquipmentType();
        if (selectedTypeId != null && selectedTypeId > 0) {
            List<Long> allTypeIds = getAllChildTypeIds(selectedTypeId);
            if (!allTypeIds.isEmpty()) {
                wrapper.in("type_id", allTypeIds);
            }
        }
        //设备型号筛选
        if (vo.getModel() != null && !vo.getModel().isEmpty()){
            System.out.println("开始查询"+vo.getModel());

            wrapper.in("model",vo.getModel());
        }
        //设备品牌筛选
        if (!StringUtils.isEmpty(vo.getBrand())){
            System.out.println("开始查询"+vo.getBrand());
            wrapper.eq("brand",vo.getBrand());
        }
        //价格筛选
        if (vo.getMinPrice()!=null) {
            System.out.println("开始查询"+vo.getMinPrice());
            wrapper.ge("expected_price", vo.getMinPrice());
        }
        if (vo.getMaxPrice()!=null) {
            System.out.println("开始查询"+vo.getMaxPrice());
            wrapper.le("expected_price", vo.getMaxPrice());
        }
        //时间筛选
        List<String> timeRanges = vo.getReleaseTime();
        if (timeRanges != null && !timeRanges.isEmpty()) {
            System.out.println("开始查询"+timeRanges);
            LocalDateTime endTime = LocalDateTime.now();
            wrapper.and(w -> {
                for (String timeRangeValue : timeRanges) {
                    LocalDateTime startTime = null;
                    boolean isOverThreeMonths = false;
                    switch (timeRangeValue) {
                        case PurchaseDemandDto.ReleaseTime.ONE_DAY:
                            startTime = endTime.minusDays(1);
                            break;
                        case PurchaseDemandDto.ReleaseTime.THREE_DAYS:
                            startTime = endTime.minusDays(3);
                            break;
                        case PurchaseDemandDto.ReleaseTime.ONE_WEEK:
                            startTime = endTime.minusWeeks(1);
                            break;
                        case PurchaseDemandDto.ReleaseTime.ONE_MONTH:
                            startTime = endTime.minusMonths(1);
                            break;
                        case PurchaseDemandDto.ReleaseTime.THREE_MONTHS:
                            startTime = endTime.minusMonths(3);
                            break;
                        case PurchaseDemandDto.ReleaseTime.MORE_THAN_THREE_MONTHS:
                            startTime = endTime.minusMonths(3);
                            isOverThreeMonths = true;
                            break;
                        default:
                            continue;
                    }
                    if (startTime!=null){
                        if (isOverThreeMonths){
                            w.or().le("release_time", startTime);
                        }else {
                            w.or().ge("release_time", startTime);
                        }
                    }

                }
            });
        }
        //排序
        if (!StringUtils.isEmpty(vo.getSortType())){
            switch (vo.getSortType()){
                case PurchaseDemandDto.SortType.expected_price_ASC:
                    wrapper.orderByAsc("expected_price");
                    break;
                case PurchaseDemandDto.SortType.expected_price_DESC:
                    wrapper.orderByDesc("expected_price");
                    break;
                case PurchaseDemandDto.SortType.release_time_DESC:
                    wrapper.orderByDesc("release_time");
                    break;
                case PurchaseDemandDto.SortType.release_time_ASC:
                    wrapper.orderByAsc("release_time");
                    break;
                case PurchaseDemandDto.SortType.expected_year_ASC:
                    wrapper.orderByAsc("expected_year");
                    break;
                case PurchaseDemandDto.SortType.expected_year_DESC:
                    wrapper.orderByDesc("expected_year");
                    break;
                default:
                    wrapper.orderByDesc("create_time");
                    break;
            }
        }else {
            wrapper.orderByDesc("create_time");

        }
        List<PurchaseDemand> purchaseDemands = purchaseDemandService.list(wrapper);


        PageInfo<PurchaseDemand> purchaseDemandPageInfo = new PageInfo<>(purchaseDemands);
        System.out.println("查询到的结果 = " + purchaseDemandPageInfo);
        return purchaseDemandPageInfo;
    }
}
